package demo.gui;

import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class OpenFile {
	private Frame f;
	private MenuBar bar;
	private Menu fileMenu;
	private MenuItem open, save, close;
	private FileDialog d1, d2;
	private Dialog d;
	private TextArea ta;
	private File file = null;
	private Button b1, b2;

	public OpenFile() {
		init();
	}

	private void init() {
		f = new Frame("�ļ�����");
		f.setBounds(600, 500, 400, 300);
		ta = new TextArea();
		f.add(ta);
		bar = new MenuBar();
		fileMenu = new Menu("�ļ�");
		open = new MenuItem("��");
		save = new MenuItem("����");
		close = new MenuItem("�ر�");
		f.setMenuBar(bar);
		bar.add(fileMenu);
		fileMenu.add(open);
		fileMenu.add(save);
		fileMenu.add(close);
		addEvent();
		f.setVisible(true);
	}

	private void addEvent() {
		f.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				if (file == null)
					System.exit(0);
				else
					dialog(0);
			}
		});
		open.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (file != null)
					dialog(1);
				d1 = new FileDialog(f, "��Ҫ��", FileDialog.LOAD);
				d1.setVisible(true);
				String path = d1.getDirectory();
				String name = d1.getFile();
				if (path == null || name == null)
					return;
				ta.setText("");
				try {
					file = new File(path, name);
					BufferedReader bf = new BufferedReader(new FileReader(file));
					String line = null;
					while ((line = bf.readLine()) != null)
						ta.append(line + "\t\n");
					bf.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		});
		save.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				d2 = new FileDialog(f, "��Ҫ����", FileDialog.SAVE);
				save();
			}
		});
		close.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dialog(1);
			}
		});
		ta.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
			}
		});
	}

	public void save() {
		if (file == null) {
			d2.setVisible(true);
			String path = d2.getDirectory();
			String name = d2.getFile();
			if (path == null || name == null)
				return;
			file = new File(path, name);

		}
		try {
			BufferedWriter bw = new BufferedWriter(new FileWriter(file));
			String text = ta.getText();
			bw.write(text);
			bw.close();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		file = null;
	}

	public void dialog(int i) {
		if (file == null)
			return;
		d = new Dialog(f, "�Ƿ񱣴棿", true);
		d.setBounds(650, 550, 100, 100);
		d.setLayout(new FlowLayout());
		b1 = new Button("���沢�ر�");
		b2 = new Button("������ֱ�ӹر�");
		d.add(b1);
		d.add(b2);
		b1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				save();
				d.setVisible(false);
			}
		});
		b2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				d.setVisible(false);
			}
		});
		d.setVisible(true);
		if (i == 0)
			System.exit(0);
	}

	public static void main(String[] args) {
		new OpenFile();
	}

}
